﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {

            lblError.Text = "";
            lblError.Visible = false;
            bindDynamicGrid();
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
            lblError.Visible = true;
        }
    }

    private void bindDynamicGrid()
    {
        DataTable Dt = null;
        try
        {
            lblError.Text = "";
            lblError.Visible = false;
            Dt = getTable();
            ShowDynamicGrids(Dt, 2);
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
            lblError.Visible = true;
        }
    }

    private DataTable getTable()
{
        Purchase.Purchase tt=new Purchase.Purchase();
        
       // DataTable dt1=
//
//
//Here You Can call you Database Function as Example i'm Generation Some Sample DataTable
//
DataTable dt=null;
try
{
dt=new DataTable();
dt = tt.test();
dt.Columns.Add("Id",typeof(int));
dt.Columns.Add("Name",typeof(string));
//dt.Columns.Add("DupName",typeof(string));
DataRow dr=null;
for(int i=0;i<2;i++)
{
dr=dt.NewRow();
dr[0]=i.ToString();
dr[1] = "UD_UserPersonalID " + i.ToString();
//dr[2] = "UD_UserPersonalID " + i.ToString();
dt.Rows.Add(dr);
dr=null;
}
return dt;
}
catch(Exception ex)
{
throw ex;
}
finally
{
if(dt!=null)
dt=null;
}
}

    void grdDyanmic_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
GridView grdPaging=null;
try
{
grdPaging=(GridView)sender;
grdPaging.PageIndex=e.NewPageIndex;
//
//This Line We add for Dynamic Grid as we don't know for which grid client has selected paging so we again assign datasource property for the selected grid
grdPaging.DataSource=(DataTable)grdPaging.DataSource;
grdPaging.DataBind();
//grdPaging.HeaderText.Cells[0].Text="Sl.No";
grdPaging.Columns[2].Visible=false;
}
catch(Exception ex)
{
lblError.Text=ex.Message;
lblError.Visible=true;
}
finally
{
if(grdPaging!=null)
grdPaging=null;
}
    }
    private void ShowDynamicGrids(DataTable dt, int Count)
    {
        HtmlTableRow tr = null;
        HtmlTableCell td = null;
        GridView grdDynamic = null;
        try
        {
            for (int i = 1; i <= Count; i++)
            {
                grdDynamic = new GridView();

                grdDynamic.AutoGenerateColumns = false;
                grdDynamic.AllowPaging = true;
                grdDynamic.AllowSorting = false;
                grdDynamic.PagerSettings.Position = PagerPosition.Bottom;
                grdDynamic.PagerSettings.Mode = PagerButtons.Numeric;
                grdDynamic.PagerSettings.Visible = true;
                grdDynamic.ShowFooter = false;
                grdDynamic.PagerSettings.PageButtonCount = 2;
                grdDynamic.PageSize = 2;
                grdDynamic.PagerSettings.Position = PagerPosition.Bottom;
                grdDynamic.PagerSettings.Mode = PagerButtons.Numeric;
                grdDynamic.HeaderStyle.CssClass = "GridHeaderStyle";
                grdDynamic.RowStyle.CssClass = "GridRowStyle";
                grdDynamic.AlternatingRowStyle.CssClass = "GridAltRowStyle";
                BoundField bound = null;
                foreach (DataColumn dc in dt.Columns)
                {
                    bound = new BoundField();
                    bound.DataField = dc.ColumnName;
                    bound.HeaderText = dc.ColumnName;
                    grdDynamic.Columns.Add(bound);
                    bound = null;
                }
                //This Line is Very Important for Dynamic Grid Paging
                this.Controls.Add(grdDynamic);
                //
                //
                //Suppose if you Want Select linkButton you can Add this Line
                //grdDyanmic.AutoGenerateSelectButton=true;
                //
                grdDynamic.PageIndexChanging += new GridViewPageEventHandler(grdDyanmic_PageIndexChanging);
                grdDynamic.DataSource = dt;
                grdDynamic.DataBind();
                grdDynamic.HeaderRow.Cells[0].Text = "Sl.No";
                grdDynamic.Columns[2].Visible = false;
                tr = new HtmlTableRow();
                td = new HtmlTableCell();
                td.Controls.Add(grdDynamic);
                tr.Cells.Add(td);
                tblDynamic.Rows.Add(tr);
                tr = null;
                td = null;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (tr != null)
                tr = null;
            if (td != null)
                td = null;
            if (grdDynamic != null)
                grdDynamic = null;
        }
    }

}
